﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RST
{
    public class Certificate : BaseRST
    {
        protected override Model.RST<T> GetRSTObject<T>(string Action, object Query)
        {
            return GetRSTObject<T>("Certificate", Action, Query);
        }

        public bool Login(Model.Certificate c)
        {
            try
            {
                Model.RST<Model.Certificate> rsp = GetRSTObject<Model.Certificate>("login", c);
                if (rsp.RSTCode == Model.RSTStatusCode.OK)
                {
                    if (rsp.RSTData != null)
                    {
                        c.Id = rsp.RSTData.Id;
                        c.CreateDate = rsp.RSTData.CreateDate;
                        c.ModifyDate = rsp.RSTData.ModifyDate;
                        c.Permissions=rsp.RSTData.Permissions;
                        c.Status = rsp.RSTData.Status;
                        //rsp.RSTData.Password = c.Password;
                    }
                    //Core.Session.Certificate = rsp.RSTData;
                    return true;
                }
            }
            catch (Exception ex)
            {
            }
            return false;
        }

        public List<Model.Certificate> List(Model.Search.CertificateList scl)
        {
            Model.RST<List<Model.Certificate>> rsp = GetRSTObject<List<Model.Certificate>>("arraylist", scl);
            return rsp.RSTData;
        }

        public Model.Certificate Insert(Model.Certificate c)
        {
            Model.RST<Model.Certificate> rsp = GetRSTObject<Model.Certificate>("insert", c);
            if (rsp.RSTCode == Model.RSTStatusCode.OK)
            {
                c.Id = rsp.RSTData.Id;
                c.CreateDate = rsp.RSTData.CreateDate;
                c.ModifyDate = rsp.RSTData.ModifyDate;
            }
            return c;
        }

        public bool Update(Model.Certificate c)
        {
            Model.RST<int> rsp = GetRSTObject<int>("update", c);
            if (rsp.RSTCode == Model.RSTStatusCode.OK)
            {
                if (rsp.RSTData > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public bool ChangePassword(Model.Search.CertificateChangePassword sccp)
        {
            Model.RST<bool> rsp = GetRSTObject<bool>("changepassword", sccp);
            if (rsp.RSTCode == Model.RSTStatusCode.OK)
            {
                return rsp.RSTData;
            }
            else
            {
                return false;
            }
        }

        public bool Delete(Model.Certificate c)
        {
            Model.RST<int> rsp = GetRSTObject<int>("delete", c);
            if (rsp.RSTCode == Model.RSTStatusCode.OK && rsp.RSTData > 0)
            {
                if (rsp.RSTData > 0)
                    return true;
            }
            return false;
        }
    }
}